// 保留俩位小数
export function keepTwoDecimalFull(num) {
  let result = parseFloat(num)
  if (isNaN(result)) {
    console.log('传递参数错误，请检查！')
    return false
  }
  result = Math.round(num * 100) / 100
  let s_x = result.toString()
  let pos_decimal = s_x.indexOf('.')
  if (pos_decimal < 0) {
    pos_decimal = s_x.length
    s_x += '.'
  }
  while (s_x.length <= pos_decimal + 2) {
    s_x += '0'
  }
  return s_x
}
// 校验金钱
export function checkMoney(money) {
  // 如果输入非数字，则替换为''
  money = money.replace(/[^\d\.]/g, '')
  // 必须保证第一个为数字而不是.
  money = money.replace(/^\./g, '')
  // 前两位不能是0加数字
  money = money.replace(/^0\d[0-9]*/g, '')
  // 保证只有出现一个.而没有多个.
  money = money.replace(/\.{2,}/g, '.')
  // 保证.只出现一次，而不能出现两次以上
  money = money.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.')
  // 只能输入两位小数
  money = money.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
  return money
}
// 格式化金钱
export function formatNumber(s, n) {
  n = n > 0 && n <= 20 ? n : 2
  s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(n) + ''
  let l = s.split('.')[0].split('').reverse()
  let r = s.split('.')[1]
  let t = ''
  for (let index = 0; index < l.length; index++) {
    t +=
      l[index] + ((index + 1) % 3 === 0 && index + 1 !== l.length ? ',' : '')
  }
  return t.split('').reverse().join('') + '.' + r
}

// 减法
export function sub(num1, num2) {
  var r1, r2, m
  try {
    r1 = num1.toString().split('.')[1].length
  } catch (e) {
    r1 = 0
  }
  try {
    r2 = num2.toString().split('.')[1].length
  } catch (e) {
    r2 = 0
  }
  m = Math.pow(10, Math.max(r1, r2))
  let n = r1 >= r2 ? r1 : r2
  return (Math.round(num1 * m - num2 * m) / m).toFixed(n)
}
// 加法
export function add(...val) {
  let max = 0
  let count = 0
  for (let i = 0; i < val.length; i++) {
    const strVal = val[i].toString()
    const index = strVal.indexOf('.')
    let num = 0
    if (index > -1) {
      num = strVal.length - 1 - index
      max = num > max ? num : max
    }
  }
  for (let i = 0; i < val.length; i++) {
    count += Math.round(val[i] * Math.pow(10, max))
  }
  return count / Math.pow(10, max)
}

// 乘法
export function ride(...val) {
  const strVal = val[0].toString()
  const strValTwo = val[1].toString()
  const index = strVal.indexOf('.')
  const indexTwo = strValTwo.indexOf('.')
  const num = [0, 0]
  if (index > -1) {
    num[0] = strVal.length - 1 - index
  }
  if (indexTwo > -1) {
    num[1] = strValTwo.length - 1 - index
  }
  return (
    Math.round(
      val[0] * Math.pow(10, num[0]) * (val[1] * Math.pow(10, num[1]))
    ) / Math.pow(10, num[0] + num[1])
  )
}

// 除法
export function exc(val, valTwo = 100) {
  const strVal = val.toString()
  const strValTwo = valTwo.toString()
  const index = strVal.indexOf('.')
  const indexTwo = strValTwo.indexOf('.')
  const num = [0, 0]
  if (index > -1) {
    num[0] = strVal.length - 1 - index
  }
  if (indexTwo > -1) {
    num[1] = strValTwo.length - 1 - index
  }
  return (
    Math.round(val * Math.pow(10, num[0])) /
    (Math.round(valTwo * Math.pow(10, num[1])) * Math.pow(10, num[0] - num[1]))
  )
}
